package edu.moravian.entity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

/**
 *
 * @author ronaghan
 */
public class BallProperties
{
    //The location of the ball properties file 
    private static final String FILE_PATH = "src/edu/moravian/resources/BallProperties.ini";

    
    //The String keys for the properties
    private static final String MIN_SPEED_KEY = "MinSpeed";
    private static final String MAX_SPEED_KEY = "MaxSpeed";
    private static final String MIN_RADIUS_KEY = "MinRadius";
    private static final String MAX_RADIUS_KEY = "MaxRadius";
    private static final String NUM_BALLS_KEY = "NumBalls";
    private static final String ELASTICITY_KEY = "Elasticity";
    
    //The default values. These could be stored in a "safer" 
    //location that "users" can't modify, but this works for the
    //bouncing balls example
    private static final String DEFAULT_MIN_SPEED = "40";
    private static final String DEFAULT_MAX_SPEED = "80";
    private static final String DEFAULT_MIN_RADIUS = "5";
    private static final String DEFAULT_MAX_RADIUS = "50";
    private static final String DEFAULT_NUM_BALLS = "25";
    private static final String DEFAULT_ELASTICITY = "1.0"; 
   
    
    private double minSpeed;
    private double maxSpeed;
    private double minRadius;
    private double maxRadius;
    private int numBalls;
    private double elasticity;
    
    private Properties props;

    /**
     * Create a new ball properties 
     */
    public BallProperties()
    {
        
        props = new Properties();
        
        try
        {
            props.load(new FileInputStream(new File(FILE_PATH)));

        }
        catch (FileNotFoundException ex)
        {
            System.out.println("Error: setting defaults: \n " + ex.toString());

        }
        catch (IOException ex)
        {
            System.out.println("Error: setting defaults: \n " + ex.toString());
        }

        
        minSpeed = getDouble(MIN_SPEED_KEY, DEFAULT_MIN_SPEED);
        maxSpeed = getDouble(MAX_SPEED_KEY, DEFAULT_MAX_SPEED);
        minRadius = getDouble(MIN_RADIUS_KEY, DEFAULT_MIN_RADIUS);
        maxRadius = getDouble(MAX_RADIUS_KEY, DEFAULT_MAX_RADIUS);
        numBalls = getInt(NUM_BALLS_KEY, DEFAULT_NUM_BALLS);
        elasticity = getDouble(ELASTICITY_KEY, DEFAULT_ELASTICITY);

    }







    /**
     * Helper method to extract double out of the properties
     * 
     * If the specified key cannot be found, the default is used in its place
     * 
     * @param key the key to look for in the properties
     * @param defaultValue the default value if the key wasn't found 
     * 
     * @return the value for the key as a double if the value of the default as 
     * a double if the value for the key wasn't found
     */
    private double getDouble(String key, String defaultValue)
    {
        String tempKey = key.trim();
        
        String value = props.getProperty(tempKey, defaultValue);

        value = value.trim();
        
        double ret = Double.parseDouble(value);

        return ret;
    }



    /**
     * Helper method to extract int out of the properties
     * 
     * If the specified key cannot be found, the default is used in its place
     * 
     * @param key the key to look for in the properties
     * @param defaultValue the default value if the key wasn't found 
     * 
     * @return the value for the key as an int if the value of the default as 
     * an int if the value for the key wasn't found
     */
    private int getInt(String key, String defaultValue)
    {
        String tempKey = key.trim();
        
        String value = props.getProperty(tempKey, defaultValue);

        value = value.trim();
        
        int ret = Integer.parseInt(value);

        return ret;
    }





    /**
     * Get the max radius 
     * 
     * @return 
     */
    public double getMaxRadius()
    {
        return maxRadius;
    }



    /**
    * Get the max speed
    * 
    * @return 
    */
    public double getMaxSpeed()
    {
        return maxSpeed;
    }



    /**
     * Get the min speed
     * 
     * @return 
     */
    public double getMinRadius()
    {
        return minRadius;
    }



    /**
     * Get the min speed 
     * 
     * @return 
     */
    public double getMinSpeed()
    {
        return minSpeed;
    }



    /**
     * Get the number of balls 
     * 
     * @return 
     */
    public double getNumBalls()
    {
        return numBalls;
    }



    /**
     * Get the elasticity
     * 
     * @return 
     */
    public double getElasticity()
    {
        return elasticity;
    }




}



